package entity;

import repo.PhaseDB;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author Ayase
 * @date 2020/8/9-22:08
 */
public class BrainTrain {
    private static int difficult;

    /**
     * 题目列表
     */
    private static ArrayList<Phase> phases;

    private static boolean[] result;

    private static BrainTrain instance = new BrainTrain();

    /**
     * @MethodName BrainTrain
     * @Description TODO 构造方法，默认难度为2
     * @Param []
     * @Return
     * @author Ayase
     * @date 22:22
     */
    private BrainTrain(){
        setDifficult(2);
    }

    /**
     * @MethodName getInstance
     * @Description TODO 获取实例同时确定难度
     * @Param [difficult]
     * @Return entity.BrainTrain
     * @author Ayase
     * @date 22:22
     */
    public static BrainTrain getInstance(int difficult){
        BrainTrain.difficult = difficult;
        return instance;
    }

    /**
     * @MethodName getInstance
     * @Description TODO 获取实例
     * @Param [difficult]
     * @Return entity.BrainTrain
     * @author Ayase
     * @date 22:22
     */
    public static BrainTrain getInstance(){
        return instance;
    }

    /**
     * @MethodName getDifficult
     * @Description TODO
     * @Param []
     * @Return int
     * @author Ayase
     * @date 22:22
     */
    public int getDifficult() {
        return difficult;
    }


    /**
     * @MethodName setDifficult
     * @Description TODO
     * @Param [difficult]
     * @Return void
     * @author Ayase
     * @date 22:22
     */
    public void setDifficult(int difficult) {
        BrainTrain.difficult = difficult;
    }

    public ArrayList<Phase> getPhases() {
        return phases;
    }

    /**
     * @MethodName greneratePhase
     * @Description TODO 从数据库获取题目，从中抽取难度数量的题目
     * @Param []
     * @Return void
     * @author Ayase
     * @date 22:28
     */
    public void greneratePhase(){
        phases = new ArrayList<Phase>(Arrays.asList(PhaseDB.getRandomPhase(difficult)));
//        for(int i = 0 ; i < phases.size();i++){
//            System.out.println(phases.get(i));
//        }
    }

    /**
     * @MethodName check
     * @Description TODO 返回做题是否正确
     * @Param [answers]
     * @Return boolean[]
     * @author Ayase
     * @date 8:26
     */
    public boolean[] check(String[] answers){
        result = new boolean[difficult];
        for(int i = 0 ; i < result.length ; i++){
            result[i] = phases.get(i).check(answers[i]);
        }
        return result;
    }

    /**
     * @MethodName calcuCR
     * @Description TODO 计算正确率，应先检查答案得出结果后调用本方法
     * @Param []
     * @Return double
     * @author Ayase
     * @date 8:32
     */
    public double calcuCR(){
        if(result == null){
            throw new IllegalStateException("仍未检查!结果集为null!");
        }
        int c = 0;
        for(int i = 0 ; i < result.length ; i++){
            if(result[i] == true){
                c++;
            }
        }
        return c/difficult;
    }
}
